/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kembali;

//import java.sql.CallableStatement;
import Koneksi.Koneksi;
import Pinjam.Data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
//import kategori.Kategori;

/**
 *
 * @author basisb11
 */
public class KembaliKontrol {

    private Connection conn;
//    private Kategori Kategori;

    public KembaliKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static KembaliKontrol getKoneksiKembali() throws SQLException {
        Koneksi dt = new Koneksi();
        KembaliKontrol kon = new KembaliKontrol(dt.getConnection());
        return kon;

    }

    public List<kembali> lihatseluruhKategori() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);
            statement = conn.prepareStatement("Select *"
                    + "from pinjam order by NIM");
            result = statement.executeQuery();
            List<kembali> kategoris = new ArrayList<kembali>();
            while (result.next()) {
                kembali kategori = new kembali();
                kategori.setJudulBuku(result.getString(1));
                kategori.setNIM(result.getString(2));

//                Data pinjam = new Data();
//                pinjam.setNIM(result.getString("NIM"));
//                pinjam.setTanggalPinjam(result.getString("tanggalPinjam"));
                kategoris.add(kategori);


            }
            conn.commit();
            return kategoris;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement == null) {
                    statement.close();

                }
            } catch (SQLException exception) {
                throw exception;
            }
        }

    }

    public void KembaliData(kembali dt) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "insert into mahasiswa "
                    + "(judulBuku, NIM)"
                    + "values (?,?)";

            statement = conn.prepareStatement(sql);
            statement.setString(1, dt.getJudulBuku());
            statement.setString(2, dt.getNIM());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }


        }
    }

    public kembali cariberdasarkanNIM(String NIM) throws SQLException {
        PreparedStatement stat = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);
            stat = conn.prepareStatement("Select *"
                    + "From Pinjam "
                    + "where NIM = ?");


            stat.setString(1, NIM);
            result = stat.executeQuery();
            kembali barang = null;
            if (result.next()) {
                kembali data = new kembali();
//                JOptionPane.showConfirmDialog(null,"judul" + data.getJudulBuku() + "NIM" + data.getNIM());
                data.setJudulBuku(result.getString("judulBuku"));
                data.setNIM(result.getString("NIM"));



            }
            conn.commit();
            return barang;
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;

        } finally {
            try {
                conn.setAutoCommit(true);
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    public void hapusData(kembali kembalis) throws SQLException {
        String judul = kembalis.getJudulBuku();
        String nim = kembalis.getNIM();
        String sql = "delete from pinjam where NIM = '" + nim + "'";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }

   
    }

